<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>扫描发货</title>
	    <link rel="stylesheet" href="css/AllStyle.css" />
	    <link rel="stylesheet" href="css/iconfont.css" />
	    <script type="text/javascript" src="js/common.js" ></script>
	    <script type="text/javascript" src="js/data.js" ></script>
	    <script type="text/javascript" src="js/tmpl.js" ></script>
	</head>
	<body>
		<!-- 页头 -->
	    <section>
		    <script type="text/javascript">
		        AllPageHeader()
		    </script>
	    </section>
	    
	    <!-- 页面  -->
	    <section class="AllContent">
	    	<form>
	    		<div class="FormStyle">
	    			<ul>
	    				<li>产品名称</li>
	    				<li>
	    					<select name="pid" id="pid" onchange="getProductLots()"></select>
	    				</li>
	    				<li>产品批次</li>
	    				<li>
	    					<select name="lot_no" id="lot_no"></select>
	    				</li>
	    				<li>发货网点</li>
	    				<li>
	    					<select name="from_branch" id="from_branch"></select>
	    				</li>
	    				<li>收货网点</li>
	    				<li>
	    					<select name="to_branch" id="to_branch"></select>
	    				</li>
	    				<li>初始化</li>
	    				<li>
	    					<select class="form-control" type="text" name="is_del" id="is_del">
                                <option value="否">否</option>
                                <option value="是">是</option>
                            </select>
	    				</li>
	    			</ul>
	    			
	    			<button class="NextButton" type="button" onclick="donext();">下一步</button>
	    		</div>
	    	</form>
	    </section>
	    
	    <section class="eject">
	    	<div class="shadeBack"></div>
	    	<div class="shadConten">
	    		<p><input type="text" id="query" onchange="queryBranch()" placeholder="输入查询的网点名称"></p>
    			<section class="sc_block">
    				
    			</section>
	    	</div>
	    </section>
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript">
	    mui.init();
	    var _id = "" , _st = true , state = true;
		$(function () {
            loadBranch();
            loadProduct();
        });
        
		$('.FormStyle').on("click",".android_select",function(){
			_id = $(this).attr("id");
			$('.eject').show(0);
	        
	        //弹出框左右上下居中
	        var win = $(window).height()/2;
	        var sch = $('.shadConten').css('height');
	       	var scTop = win-(parseInt(sch.substring(0,sch.length-2))/2);
	        $('.shadConten').css({top:scTop});
	        var z_sc =  $('.shadConten').css("height").substring(0,sch.length-2);
	        if(z_sc >= win*2){
	        	$('.shadConten').css({height:win*2-20,top:"10px"});
	        	$('#query').focus();
	        }
	        state = false;
		})
		
		//返回或者关闭弹窗
		mui.plusReady(function() {
			mui.back = function() {
				if(state == false){
					$('.eject').hide();
	       	 		state = true;
	       	 	}else{
	       	 		var current = plus.webview.currentWebview();
	       	 		current.close();
	       	 	}
			};
		});
		
		$('.shadeBack').click(function(){
			$('.eject').hide();
	       	state = true;
		});
		
		$('.eject').on("click",".sc_block p",function(){
			_st = false;
			if(_id == "_from_branch"){
				$('#_from_branch').text($(this).text()).attr({value:$(this).attr("value")});
				$('.eject').hide();
	       		state = true;
	       		
	       		setCookie("scan.from_bid", $(this).attr("value"));
            	setCookie("scan.from_name", $(this).text());
			}else{
				$('#_to_branch').text($(this).text()).attr({value:$(this).attr("value")});
				$('.eject').hide();
	       		state = true;
			}
			loadBranch();
		})
		
		function queryBranch() {
	        var q = $("#query").val();
	        if (empty(q)) {
	            loadBranch();
	        } else {
	            $.ajax({
	                type: "post",
	                url: getServerUrl() + "/Open/Company/catbranch",
	                dataType: 'json',
	                data: 'access_token=' + getToken()+'&name='+q,
	                success: function (json) {
	                    if(json.result == "1"){
	                        showBranch(json.branchlist);
	                    }else{
	                        showBranch(json.branchlist);
	                    }
	
	                },
	                error: function () {
	                    alertMessage(L('branch_notice_netCheck'));
	                }
	            });
	
	        }
	        var oTxt=document.getElementById("query"); 
	                oTxt.value=""; 
	    }
        
        function getProductLots(){
            var pid = $('#pid').val();
            var datas = {access_token:getToken(),pid:pid};
            $.ajax({
                url:getServerUrl()+"/Open/Company/getlotlist",
                type:'post',
                dataType:'json',
                data: datas,
                success:function(result){
                       var lotlist =result.lotlist;
                      var html= '';
                      if(!empty(lotlist)){
                          for(var i= 0;i<lotlist.length;i++){
                              html += '<option value="'+lotlist[i].lot_no+'">'+lotlist[i].lot_no+'</option>';
                          }
                      }else{
                          html += '<option value="0">没有产品批次</option>';
                      }
                    $("#lot_no").html(html);
                }
            }); 
        }
        function loadBranch() {
            $.ajax({
                type: "post",
                url: getServerUrl() + "/Open/Company/getbranchlist", //与此php页面沟通
                dataType: 'json', //从php返回的值以 JSON方式 解释
                data: 'access_token='+getToken(), //
                success: function (json) {//如果调用php成功
                    branchlist = json.branchlist;
                    showBranch(branchlist);
                },
                error:function(json){
                    alertMessage(json.error_description);
                }
            });
        }
        function loadProduct() {
            $.ajax({
                type: "post",
                url: getServerUrl() + "/Open/Company/getproductlist", //与此php页面沟通
                dataType: 'json', //从php返回的值以 JSON方式 解释
                data: 'access_token='+getToken(), //
                success: function (json) {//如果调用php成功
                    productlist = json.productlist;
                    showProduct(productlist);
                },
                error:function(json){
                    alertMessage(json.error_description);
                }
            });
        }
        
        function showBranch(clist) {
            var chtml = '';
            if (!empty(clist)) {
                for (var i = 0; i < clist.length; i++) {
                    if (!empty(clist[i])) {
                    	chtml = chtml + "<p value='" + clist[i].bid + "'>"+ clist[i].name + "</p>";
                    	     
                    }
                }
                
				$('#from_branch').parent().html("<p class='android_select' id='_from_branch' value='" + clist[0].bid + "'>"+ clist[0].name + "</p>");
				$('#to_branch').parent().html("<p class='android_select' id='_to_branch' value='" + clist[0].bid + "'>"+ clist[0].name + "</p>");
            	
            	var fromBid = getCookie("scan.from_bid");
            	var fromName = getCookie("scan.from_name");
            	if(!empty(fromBid)&&!empty(fromName)){
            		$('#_from_branch').text(fromName).attr("value",fromBid);
            	}
				
            }
            $(".sc_block").html(chtml);

            if(getUserBid()){
                $("#from_branch").val(getUserBid());
                $("#from_branch").attr('disabled','disabled');
            }
        }
        function showProduct(plist) {
            var phtml = "";
            if (!empty(plist)) {
                for (var i = 0; i < plist.length; i++) {
                    if (!empty(plist[i])) {
                        phtml = phtml + "<option value='" + plist[i].pid + "'>"
                                + plist[i].name + "</option>";
                    }
                }
            }
            $("#pid").html(phtml);
            getProductLots()
        }


        function donext() {
            if (empty($("#pid").val())) {
                alertMessage("请选择产品！");
                return;
            }
            if (empty($("#from_branch").val()) && empty($("#_from_branch").attr("value")) ) {
                alertMessage("请选择发货网点！");
                return;
            }
            if (empty($("#to_branch").val()) && empty($("#_to_branch").attr("value")) ) {
                alertMessage("请选择收货网点！");
                return;
            }
            //alert($("#pid").val()+"   "+ $("#pid").text()+"  "+$("#company").val());
			
            setCookie("scan.from_bid", $("#_from_branch").attr("value"));
            setCookie("scan.from_name", $("#_from_branch").text());
            setCookie("scan.to_bid", $("#_to_branch").attr("value"));
            setCookie("scan.to_name", $("#_to_branch").text());
            
            setCookie("scan.pid", $("#pid").val());
            setCookie("scan.lot_no",$("#lot_no").val());
            setCookie("scan.is_del",$("#is_del").val());
            setCookie("scan.pname", $("#pid").find("option:selected").text());
            $("#query").val("");

			  mui.openWindow({
			    url: 'scan2.html', 
			    id:'scan2.html'
			  });
        }

        function onDeviceReady() {
            document.addEventListener("backbutton", onBackKeyDown, false);
        }
        function onBackKeyDown() {
            openUrl("index.html");
        }
	</script>
</html>
